home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / SAMPLE.CC < prev    next >
C/C++ Source or Header  |  1992-03-30  |  5KB  |  233 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Dirk Grunwald (grunwald@cs.uiuc.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #ifdef __GNUG__
  19. #pragma implementation
  20. #endif
  21. #include <stream.h>
  22. #include <SmplStat.h>
  23. #include <math.h>
  24.  
  25. // error handling
  26.  
  27. void default_SampleStatistic_error_handler(const char* msg)
  28. {
  29.   cerr << "Fatal SampleStatistic error. " << msg << "\n";
  30.   exit(1);
  31. }
  32.  
  33. one_arg_error_handler_t SampleStatistic_error_handler = default_SampleStatistic_error_handler;
  34.  
  35. one_arg_error_handler_t set_SampleStatistic_error_handler(one_arg_error_handler_t f)
  36. {
  37.   one_arg_error_handler_t old = SampleStatistic_error_handler;
  38.   SampleStatistic_error_handler = f;
  39.   return old;
  40. }
  41.  
  42. void SampleStatistic::error(const char* msg)
  43. {
  44.   (*SampleStatistic_error_handler)(msg);
  45. }
  46.  
  47. // t-distribution: given p-value and degrees of freedom, return t-value
  48. // adapted from Peizer & Pratt JASA, vol63, p1416
  49.  
  50. double tval(double p, int df) 
  51. {
  52.   double t;
  53.   int positive = p >= 0.5;
  54.   p = (positive)? 1.0 - p : p;
  55.   if (p <= 0.0 || df <= 0)
  56.     t = HUGE;
  57.   else if (p == 0.5)
  58.     t = 0.0;
  59.   else if (df == 1)
  60.     t = 1.0 / tan((p + p) * 1.57079633);
  61.   else if (df == 2)
  62.     t = sqrt(1.0 / ((p + p) * (1.0 - p)) - 2.0);
  63.   else
  64.   {    
  65.     double ddf = df;
  66.     double a = sqrt(log(1.0 / (p * p)));
  67.     double aa = a * a;
  68.     a = a - ((2.515517 + (0.802853 * a) + (0.010328 * aa)) /
  69.              (1.0 + (1.432788 * a) + (0.189269 * aa) +
  70.               (0.001308 * aa * a)));
  71.     t = ddf - 0.666666667 + 1.0 / (10.0 * ddf);
  72.     t = sqrt(ddf * (exp(a * a * (ddf - 0.833333333) / (t * t)) - 1.0));
  73.   }
  74.   return (positive)? t : -t;
  75. }
  76.  
  77. void
  78. SampleStatistic::reset()
  79. {
  80.     n = 0; x = x2 = 0.0;
  81.     maxValue = -HUGE;
  82.     minValue = HUGE;
  83. }
  84.  
  85. void
  86. SampleStatistic::operator+=(double value)
  87. {
  88.     n += 1;
  89.     x += value;
  90.     x2 += (value * value);
  91.     if ( minValue > value) minValue = value;
  92.     if ( maxValue < value) maxValue = value;
  93. }
  94.  
  95. double
  96. SampleStatistic::mean()
  97. {
  98.     if ( n > 0) {
  99.     return (x / n);
  100.     }
  101.     else {
  102.     return ( 0.0 );
  103.     }
  104. }
  105.  
  106. double
  107. SampleStatistic::var()
  108. {
  109.     if ( n > 1) {
  110.     return(( x2 - ((x * x) /  n)) / ( n - 1));
  111.     }
  112.     else {
  113.     return ( 0.0 );
  114.     }
  115. }
  116.  
  117. double
  118. SampleStatistic::stdDev()
  119. {
  120.     if ( n <= 0 || this -> var() <= 0) {
  121.     return(0);
  122.     } else {
  123.     return( (double) sqrt( var() ) );
  124.     }
  125. }
  126.  
  127. double
  128. SampleStatistic::confidence(int interval)
  129. {
  130.   int df = n - 1;
  131.   if (df <= 0) return HUGE;
  132.   double t = tval(double(100 + interval) * 0.005, df);
  133.   if (t == HUGE)
  134.     return t;
  135.   else
  136.     return (t * stdDev()) / sqrt(double(n));
  137. }
  138.  
  139. double
  140. SampleStatistic::confidence(double p_value)
  141. {
  142.   int df = n - 1;
  143.   if (df <= 0) return HUGE;
  144.   double t = tval((1.0 + p_value) * 0.5, df);
  145.   if (t == HUGE)
  146.     return t;
  147.   else
  148.     return (t * stdDev()) / sqrt(double(n));
  149. }
  150.  
  151.  
  152. #include <SmplHist.h>
  153.  
  154. const int SampleHistogramMinimum = -2;
  155. const int SampleHistogramMaximum = -1;
  156.  
  157. SampleHistogram::SampleHistogram(double low, double high, double width)
  158. {
  159.     if (high < low) {
  160.     double t = high;
  161.     high = low;
  162.     low = t;
  163.     }
  164.  
  165.     if (width == -1) {
  166.     width = (high - low) / 10;
  167.     }
  168.  
  169.     howManyBuckets = int((high - low) / width) + 2;
  170.     bucketCount = new int[howManyBuckets];
  171.     bucketLimit = new double[howManyBuckets];
  172.     double lim = low;
  173.     for (int i = 0; i < howManyBuckets; i++) {
  174.     bucketCount[i] = 0;
  175.     bucketLimit[i] = lim;
  176.     lim += width;
  177.     }
  178.     bucketLimit[howManyBuckets-1] = HUGE;    /* from math.h */
  179. }
  180.  
  181. SampleHistogram::~SampleHistogram()
  182. {
  183.     if (howManyBuckets > 0) {
  184.     delete bucketCount;
  185.     delete bucketLimit;
  186.     }
  187. }
  188.  
  189. void
  190. SampleHistogram::operator+=(double value)
  191. {
  192.     int i;
  193.     for (i = 0; i < howManyBuckets; i++) {
  194.     if (value < bucketLimit[i]) break;
  195.     }
  196.     bucketCount[i]++;
  197.     this->SampleStatistic::operator+=(value);
  198. }
  199.  
  200. int
  201. SampleHistogram::similarSamples(double d)
  202. {
  203.     int i;
  204.     for (i = 0; i < howManyBuckets; i++) {
  205.     if (d < bucketLimit[i]) return(bucketCount[i]);
  206.     }
  207.     return(0);
  208. }
  209.  
  210. void
  211. SampleHistogram::printBuckets(ostream& s)
  212. {
  213.     for(int i = 0; i < howManyBuckets; i++) {
  214.     if (bucketLimit[i] >= HUGE) {
  215.         s << "< max : " << bucketCount[i] << "\n";
  216.     } else {
  217.         s << "< " << bucketLimit[i] << " : " << bucketCount[i] << "\n";
  218.     }
  219.     }
  220. }
  221.  
  222. void
  223. SampleHistogram::reset()
  224. {
  225.     this->SampleStatistic::reset();
  226.     if (howManyBuckets > 0) {
  227.     for (register int i = 0; i < howManyBuckets; i++) {
  228.         bucketCount[i] = 0;
  229.     }
  230.     }
  231. }
  232.  
  233.